{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The `__format__` Method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We saw before the use of `__str__` and `__repr__`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However we have one more formatting function to look at!\n",
    "\n",
    "The `format()` function."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example we can use `format()` with a format specification for floats:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.10000000000000000555'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(a, '.20f')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Or we can use it with a datetime object:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "now = datetime.utcnow()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2019, 6, 13, 3, 43, 48, 904829)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Thu 2019-06-13  03:43 AM'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(now, '%a %Y-%m-%d  %I:%M %p')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can implement support for format specifiers in our own classes by implementing the `__format__` method.\n",
    "\n",
    "This is actually quite complicated to do, so we usually delegate back to some other type's formatting.\n",
    "\n",
    "Just like with `__str__` and `__repr__`, `__format__` should return a string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Person:\n",
    "    def __init__(self, name, dob):\n",
    "        self.name = name\n",
    "        self.dob = dob\n",
    "        \n",
    "    def __repr__(self):\n",
    "        print('__repr__ called...')\n",
    "        return f'Person(name={self.name}, dob={self.dob.isoformat()})'\n",
    "    \n",
    "    def __str__(self):\n",
    "        print('__str__ called...')\n",
    "        return f'Person({self.name})'\n",
    "    \n",
    "    def __format__(self, date_format_spec):\n",
    "        print(f'__format__ called with {repr(date_format_spec)}...')\n",
    "        dob = format(self.dob, date_format_spec)\n",
    "        return f'Person(name={self.name}, dob={dob})'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So now have:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "\n",
    "p = Person('Alex', date(1900, 10, 20))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__str__ called...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Person(Alex)'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__repr__ called...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Person(name=Alex, dob=1900-10-20)'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "repr(p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__format__ called with '%B %d, %Y'...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Person(name=Alex, dob=October 20, 1900)'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(p, '%B %d, %Y')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we do not specify a format, then the `format` function will use an empty string:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__format__ called with ''...\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Person(name=Alex, dob=1900-10-20)'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "format(p)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
